//
//  EnjoyDataManager.m
//  FreeReading
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "EnjoyDataManager.h"

@implementation EnjoyDataManager

+(instancetype)shareManager
{
    static EnjoyDataManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        manager = [[[self class]alloc] init];

    });
    return manager;
}

-(CGFloat)getCachesSize
{
    CGFloat size = 0;
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
    
    for (NSString *p in files) {
        
        NSError *error;

        NSString *path = [cachPath stringByAppendingPathComponent:p];
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            
            size += [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error].fileSize;
            
        }
        
    }
    return size / 1024.0 / 1024.0;
}

-(void)cleanCachesInView:(UIView *)view
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       
                       
                       for (NSString *p in files) {
                           
                           NSError *error;
                           
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                               
                           }
                       }
                   });
    
    dispatch_async(dispatch_get_main_queue(), ^{
       
        [self cleanCachesSuccussInView:view];
    });
    
}

- (void)cleanCachesSuccussInView:(UIView *)view
{
    [HUDHeaper addHUDInView:view text:@"清理成功" hideAfterDelay:1.0];
    
}

@end

